package com.example.sun.myapplication.fragment;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;

import com.example.sun.myapplication.R;
import com.example.sun.myapplication.adapter.CommonAdapter;
import com.example.sun.myapplication.adapter.ViewHolder;
import com.example.sun.myapplication.bean.WeChatMessageBean;

import java.util.ArrayList;
import java.util.List;

public class MyFragment extends Fragment {

    ListView lvMessageList;

    private CommonAdapter<WeChatMessageBean> mAdapter;
    private List<WeChatMessageBean> mMessageList;

    // 初始化Fragment的布局
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
        View contentView = inflater.inflate(R.layout.fragment_wechat_message, null);
        lvMessageList = contentView.findViewById(R.id.lv_message_list);

        mMessageList = new ArrayList<>();

        WeChatMessageBean bean = new WeChatMessageBean();
        bean.setAvatarResId(R.mipmap.avatar);
        bean.setName("张三");
        bean.setLastMessage("Hello");
        bean.setLastTime("08:30");

        mMessageList.add(bean);

        bean = new WeChatMessageBean();
        bean.setAvatarResId(R.mipmap.avatar);
        bean.setName("李四");
        bean.setLastMessage("World");
        bean.setLastTime("09:30");

        mMessageList.add(bean);

        mAdapter = new CommonAdapter<WeChatMessageBean>(getContext(), mMessageList, R.layout.item_wechat_message) {
            @Override
            public void convert(ViewHolder helper, WeChatMessageBean item) {
                helper.setImageResource(R.id.item_avatar, item.getAvatarResId());
                helper.setText(R.id.item_name, item.getName());
                helper.setText(R.id.item_last_message, item.getLastMessage());
                helper.setText(R.id.item_last_time, item.getLastTime());
            }
        };

        lvMessageList.setAdapter(mAdapter);

        return contentView;
    }

    // 与Fragment绑定的Activity的onCreate方法已经执行完成时，回调此方法
    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        // 可使用Activity调用方的相关方法
//        getActivity()
    }

    // 与Activity相同
    @Override
    public void onResume() {
        super.onResume();
    }

    // 与Activity相同
    @Override
    public void onPause() {
        super.onPause();
    }

    // 销毁与Fragment相关的视图
    @Override
    public void onDestroyView() {
        super.onDestroyView();
    }


    @Override
    public void onDestroy() {
        super.onDestroy();
    }
}
